package com.example.doctorstation.Config;

import com.jcraft.jsch.JSch;
import com.jcraft.jsch.Session;
import org.springframework.stereotype.Component;

import java.util.Properties;

@Component
public class SSHConnection {
    private final static String LocalHost = "localhost";
    private final static int LOCAl_PORT = 54467;
    private final static String SSH_REMOTE_SERVER = "8.129.218.80";
    private final static String SSH_USER = "root";
    private final static String SSH_PASSWORD = "Bishe111@";
    private final static int SSH_REMOTE_PORT = 22;
    private final static String MYSQL_REMOTE_SERVER = "localhost";
    private final static int MYSQL_REMOTE_PORT = 3306;
    private final Session   session; //represents each ssh session

    public void closeSSH ()
    {
        System.out.println("kill me");
        session.disconnect();
    }

    public SSHConnection () throws Throwable
    {
        JSch jsch = new JSch();

        session = jsch.getSession(SSH_USER, SSH_REMOTE_SERVER, SSH_REMOTE_PORT);
        session.setPassword(SSH_PASSWORD);

        Properties config = new Properties();
        config.put("StrictHostKeyChecking", "no");
        session.setConfig(config);

        session.connect(); //ssh connection established!
        //by security policy, you must connect through a fowarded port
        //int myport = sesion.setPortForwardingL(0, MYSQL_REMOTE_SERVER, MYSQL_REMOTE_PORT);
        int myport = session.setPortForwardingL(54444, MYSQL_REMOTE_SERVER, MYSQL_REMOTE_PORT);
        System.out.println(myport);
    }
}
